import axios from 'axios';
// import { Message } from 'element-ui'
import {
  ServerError500,
  ServerError403,
  ServerError10400,
  RequestError,
} from '../Error';
import Vue from 'vue';
import qs from 'qs';
const _this = new Vue();
import cookie from './cookie'
export default class Http {
  BASEURL = process.env.VUE_APP_BASE_URL;
  IMGURL = process.env.VUE_APP_BASR_IMG_URL;
  constructor() { }
  async get(ctx) {
    try {
      ctx.url = 'proxyServer' + ctx.url;
      //get的请求的参数用params接受
      ctx.params = ctx.data;
      delete ctx.data;
      ctx.method = 'get';
      ctx.headers = {
        Accept: 'application/json',
        Authorization: cookie.readCookie('userCode') || '',
        ...ctx.headers,
      };
      ctx.timeout = process.env.VUE_APP_ROUTER_TIME;
      ctx.before = ctx.before || ((ctx) => ctx); //局部请求拦截器
      ctx.after = ctx.after || ((ctx) => ctx); //局部响应拦截器
      ctx.errorCatch = ctx.errorCatch || ((err) => err); //局部异常拦截器拦截
      ctx = this.globalCatchRequest(ctx); //请求前通过全局请求拦截器
      ctx = ctx.before(ctx);
      let res = await axios.request(ctx);
      let resByGlo = this.globalCatch(res); //全局拦截后的res
      let resFinal = ctx.after(resByGlo);
      return resFinal;
    } catch (err) {
      // let errBylocal = ctx.errorCatch(err)
      // //局部拦截后的res
      // let errByGlo = this.globalErrorCatch(errBylocal)
      // return Promise.reject(errByGlo)
      //全局拦截后的res;
    }
  }
  async post(ctx) {
    try {
      ctx.url = 'proxyServer' + ctx.url;
      ctx.method = 'POST';
      ctx.headers = {
        Accept: 'application/json',
        Authorization: cookie.readCookie('userCode') || '',
        ...ctx.headers,
      };
      ctx.data = qs.stringify(ctx.data, {
        arrayFormat: 'repeat',
        allowDots: true,
        allowDots: true,
      });
      // ctx.params = ctx.data
      // delete ctx.data
      ctx.timeout = process.env.VUE_APP_ROUTER_TIME;
      ctx.before = ctx.before || ((ctx) => ctx); //局部请求拦截器
      ctx.after = ctx.after || ((ctx) => ctx); //局部响应拦截器
      ctx.errorCatch = ctx.errorCatch || ((err) => err); //局部异常拦截器拦截
      ctx = this.globalCatchRequest(ctx); //请求前通过全局请求拦截器
      ctx = ctx.before(ctx);
      let res = await axios.request(ctx);
      let resByGlo = this.globalCatch(res); //全局拦截后的res
      let resFinal = ctx.after(resByGlo);
      return resFinal;
    } catch (err) {
      // let errBylocal = ctx.errorCatch(err)
      // //局部拦截后的res
      // let errByGlo = this.globalErrorCatch(errBylocal)
      // return Promise.reject(errByGlo)
      //全局拦截后的res;
    }
  }
  globalErrorCatch(err) {
    // window.loading.close();
    console.warn(err);
    throw new RequestError(err);
    return err;
  }
  globalCatch(res) {
    // window.loading.close();
    let code = res.data.code;
    let message = res.data.message;
    switch (code) {
      case 200:
        return res;
      // break;
      case 500:
        throw new ServerError500(message);
      // break;
      case 403:
        throw new ServerError403();
      // break;
      case 10400:
        throw new ServerError10400(res.data.content);
      // break;

      default:
        break;
    }

    return res;
  }
  globalCatchRequest(ctx) {
    //  window.loading = _this.$loading();
    // setTimeout(() => {
    //     loading.close();
    // }, 2000);
    //对请求前的内容处理，返回处理后的内容
    let token = cookie.readCookie('userCode') || '';
    if (token) {
      ctx.headers['token'] = token;
    }
    //请求拦截器
    return ctx;
  }
  setImg(listOrObj, key) {
    //改变元数据
    if (Object.prototype.toString.call(listOrObj) === '[object Object]') {
      listOrObj['_' + key] = this.IMGURL + listOrObj[key];
    }
    if (Object.prototype.toString.call(listOrObj) === '[object Array]') {
      listOrObj.map((val) => {
        val['_' + key] = this.IMGURL + val[key];
      });
    }
  }
}

